package com.xiaohu.demo.utils.longLink.server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.timeout.IdleStateHandler;

public class server {
	public static void main(String[] args) {
		loadServer();
	}
	
	public static void loadServer() {
		EventLoopGroup bossGroup = new NioEventLoopGroup(1);

		EventLoopGroup workerGroup = new NioEventLoopGroup(4);
		try {
			ServerBootstrap serverBootstrap = new ServerBootstrap();

			serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).localAddress(8081)
					.childHandler(new ChannelInitializer<Channel>() {

						@Override
						protected void initChannel(Channel ch) throws Exception {
							// TODO Auto-generated method stub
							ChannelPipeline pipeline = ch.pipeline();
							pipeline.addLast(new IdleStateHandler(10, 0, 0));
							pipeline.addLast(new MsgPckDecode());
							pipeline.addLast(new MsgPckEncode());
							pipeline.addLast(new server3Handler());
						}
					});
			System.out.println("start server 8081 --");
			ChannelFuture sync = serverBootstrap.bind().sync();
			sync.channel().closeFuture().sync();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			// 优雅的关闭资源
			bossGroup.shutdownGracefully();
			workerGroup.shutdownGracefully();
		}
	}
}
